package ca.uwindsor.cs.deepweb.utility;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class FileSplitter {
	/**
	 * Instance of buffered reader to read the data source file
	 */
	protected BufferedReader bufferedreader_in;
	
	public FileSplitter(String file){
		InputStreamReader read = null;
		try {
			read = new InputStreamReader(new FileInputStream(
					file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		bufferedreader_in = new BufferedReader(read);
	}
	
	public void split(int step) throws Exception {
		FileOutputStream fp = new FileOutputStream(System
				.getProperty("user.dir")
				+ java.io.File.separator
				+ "content" + ".txt");
		String line = bufferedreader_in.readLine();
		while(line!=null){
			line = line +"\n";
			fp.write(line.getBytes("UTF-8"));
			step--;
			if(step!=0){
				line = bufferedreader_in.readLine();
			}
			else{
				break;
			}
		}
		fp.close();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		args = new String[2];
		args[0] = "D:\\Research\\estimation\\datasource\\enwiki-20080103-pages-articles\\pages-articles.xml";
		args[1] = "1000";
		FileSplitter fs = new FileSplitter(args[0]);
		try {
			fs.split(Integer.valueOf(args[1]));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
